import mui from 'mui'
import {productdetail, addtocar, usercar} from 'vuexF/actions'
import {getCookie} from 'api/api'
export default {
  data: function () {
    return {
      count: 1,
      attrid: 0,
      currentType: '商品',
      isShow: true,
      prods: {}
    }
  },
  props: ['prodsid', 'myindex', 'prod'],
  ready: function () {
  },
  methods: {
    closePop () {
      mui('.mui-popover').popover('hide')
    },
    subBuy () {
      var _this = this
      this.addtocar({
        uid: getCookie('user[id]'),
        id: this.prods.id,
        setId: this.attrid,
        num: this.count,
        color: {}
      }, function (id) {
        window.location.href = 'http://wechat.zh28.com/index.php?s=/Home/Ucenter/cart_confirm/cartInfo/' + encodeURIComponent(id + '|' + _this.count)
        _this.closePop()
      })
    },
    increment () {
      this.count ++
    },
    decrement () {
      if (this.count > 1) {
        this.count --
      }
    },
    selType (id, type) {
      this.attrid = id
      this.currentType = type
    },
    addshopcar () {
      var _this = this
      this.addtocar({
        uid: getCookie('user[id]'),
        id: this.prods.id,
        setId: this.attrid,
        num: this.count
      }, function (id) {
        mui.toast('添加成功')
        _this.closePop()
        _this.usercar(getCookie('user[id]'))
      })
    }
  },
  computed: {
    total () {
      if (this.count < 1) {
        this.count = 1
      }
      return parseFloat(this.prods.price) * this.count
    },
    isready () {
      if (this.prods.title) {
        this.currentType = this.prods.attr[0].title
        this.attrid = this.prods.attr[0].id
        return true
      } else {
        return false
      }
    },
    isChange () {
      if (this.prodsid) {
        if (!this.prod) {
          this.isShow = false
          var _this = this
          this.productdetail(this.prodsid, function () {
            _this.isShow = true
            _this.prods = _this.prodtem
          })
        } else {
          this.prods = this.prod
        }
        return true
      } else {
        return false
      }
    }
  },
  vuex: {
    getters: {
      prodtem: state => state.productdetail
    },
    actions: {
      productdetail,
      addtocar,
      usercar
    }
  }
}
